<template>
    <div class="views-shangpindingdan-web-add">
        <div>
            <e-container>
                <el-card class="box-card">
                    <template #header>
                        <div class="clearfix">
                            <span class="title"> 添加 </span>
                        </div>
                    </template>

                    <el-form :model="form" ref="formModel" label-width="140px" status-icon validate-on-rule-change>
                        <el-form-item label="订单号" prop="dingdanhao" :rules="[{required:true, message:'请填写订单号'}]">
                            <el-input type="text" placeholder="输入订单号" style="width: 450px" v-model="form.dingdanhao" />
                        </el-form-item>

                        <el-form-item label="订单商品" prop="dingdanshangpin">
                            <div id="dataListdingdanshangpin" style="text-align: left">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>商品编号</th>
                                            <th>商品名称</th>
                                            <th>商品分类</th>
                                            <th>商品图片</th>
                                            <th>价格</th>
                                            <th>发布人</th>
                                            <th>购买数量</th>
                                            <th>小计</th>
                                            <th>购买人</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr v-for="dataMap in dataSets">
                                            <td>{{ dataMap.shangpinbianhao }}</td>
                                            <td>{{ dataMap.shangpinmingcheng }}</td>
                                            <td><e-select-view module="shangpinfenlei" :value="dataMap.shangpinfenlei" select="id" show="fenleiming"></e-select-view></td>
                                            <td>
                                                <div style="width: 100%; max-width: 120px">
                                                    <e-img :src="dataMap.shangpintupian" type="list"></e-img>
                                                </div>
                                            </td>
                                            <td>{{ dataMap.jiage }}</td>
                                            <td>{{ dataMap.faburen }}</td>
                                            <td>{{ dataMap.goumaishuliang }}</td>
                                            <td>{{ dataMap.xiaoji }}</td>
                                            <td>{{ dataMap.goumairen }}</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-form-item>

                        <el-form-item style="display: none"  label="商家" prop="shangjia"> <el-input v-model="form.shangjia" readonly style="width: 250px"></el-input> </el-form-item>

                        <el-form-item label="总价" prop="zongjia" :rules="[{validator:rule.checkNumber, message:'输入一个有效数字'}]">
                            <el-input readonly type="number" placeholder="输入总价" style="width: 450px" v-model.number="form.zongjia" />
                        </el-form-item>

                        <el-form-item label="购买人姓名" prop="goumairenxingming" required :rules="[{required:true, message:'请填写购买人姓名'}]">
                            <el-input type="text" placeholder="输入购买人姓名" style="width: 450px" v-model="form.goumairenxingming" />
                        </el-form-item>

                        <el-form-item label="联系电话" prop="lianxidianhua" required :rules="[{required:true, message:'请填写联系电话'}]">
                            <el-input type="text" placeholder="输入联系电话" style="width: 450px" v-model="form.lianxidianhua" />
                        </el-form-item>

                        <el-form-item label="购买人" prop="goumairen"> <el-input v-model="form.goumairen" readonly style="width: 250px"></el-input> </el-form-item>

                        <el-form-item label="备注" prop="beizhu"> <el-input type="textarea" v-model="form.beizhu"></el-input> </el-form-item>

                        <el-form-item v-if="btnText">
                            <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                        </el-form-item>
                    </el-form></el-card
                >
            </e-container>
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";

    import { ref, reactive, computed, unref } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useShangpindingdanCreateForm, canShangpindingdanInsert } from "@/module";
    import { extend } from "vue-design-plugin";

    const route = useRoute();
    const props = defineProps({
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
    });
    const { form } = useShangpindingdanCreateForm();
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);

    const dataSets = ref([]);
    const loadDataSets = ()=>{
        var data = JSON.parse(route.query.shangpin);
        form.zongjia = data.xiaoji = data.jiage * data.goumaishuliang;
        dataSets.value.push(data);
        form.shangpin = data;
        form.lijigoumai = "1";
    }
    loadDataSets();



    form.targetIds = route.query.ids; // 添加提交值
    var submit = () => {
        formModel.value.validate().then((res) => {
            if (loading.value) return;
            loading.value = true;

            var price = "xiaoji";
            var shangjia = "faburen";

            var dataList = unref(dataSets);
            var result = {};
            for (var i in dataList) {
                var ci = dataList[i];
                if (!result[ci[shangjia]]) {
                    result[ci[shangjia]] = {
                        shangjia: ci[shangjia],
                        ids: [],
                        jine: 0,
                    };
                }
                result[ci[shangjia]].ids.push(ci.id);
                result[ci[shangjia]].jine += parseFloat(ci[price]);
            }

            var submitIndex = 0;
            var formData = unref(form);

            for (var i in result) {
                var ci = result[i];

                formData.dingdanhao = rule.getID();
                formData.shangjia = i;
                formData.zongjia = ci.jine;
                formData.targetIds = ci.ids.join(",");
                submitIndex++;

                canShangpindingdanInsert(formData).then(
                    (res) => {
                        submitIndex--;
                        if (submitIndex == 0) {
                            ElMessage.success("创建成功");
                            loading.value = false;
                            router.replace("/admin/shangpindingdan/goumairen");
                        }
                    },
                    (err) => {
                        loading.value = false;
                        ElMessageBox.alert(err.message);
                    }
                );
            }
        });
    };
</script>

<style scoped lang="scss">
    .views-shangpindingdan-web-add {
    }
</style>
